package flc.ast.fragment;

import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.ArticleActivity;
import flc.ast.activity.HotActivity;
import flc.ast.activity.ListDetailActivity;
import flc.ast.activity.MovieDetailActivity;
import flc.ast.activity.MovieListActivity;
import flc.ast.activity.SearchActivity;
import flc.ast.adapter.ArticleAdapter;
import flc.ast.adapter.HotAdapter;
import flc.ast.adapter.MovieListAdapter;
import flc.ast.adapter.MyBannerAdapter;
import flc.ast.databinding.FragmentHomeBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;
import stark.common.bean.StkTagResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private HotAdapter hotAdapter;
    private MovieListAdapter movieListAdapter;
    private ArticleAdapter articleAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.llSearch.setOnClickListener(this);
        mDataBinding.tvHotAll.setOnClickListener(this);
        mDataBinding.tvMovieListAll.setOnClickListener(this);
        mDataBinding.tvArticleAll.setOnClickListener(this);

        mDataBinding.rvHotList.setLayoutManager(new GridLayoutManager(mContext, 3));
        hotAdapter = new HotAdapter();
        mDataBinding.rvHotList.setAdapter(hotAdapter);
        hotAdapter.setOnItemClickListener(this);

        mDataBinding.rvMovieList.setLayoutManager(new LinearLayoutManager(mContext));
        movieListAdapter = new MovieListAdapter();
        mDataBinding.rvMovieList.setAdapter(movieListAdapter);
        movieListAdapter.setOnItemClickListener(this);

        mDataBinding.rvArticleList.setLayoutManager(new LinearLayoutManager(mContext));
        articleAdapter = new ArticleAdapter();
        mDataBinding.rvArticleList.setAdapter(articleAdapter);
        articleAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.llSearch://搜索
                startActivity(SearchActivity.class);
                break;
            case R.id.tvHotAll://热门影片
                startActivity(HotActivity.class);
                break;
            case R.id.tvMovieListAll://推荐影片
                startActivity(MovieListActivity.class);
                break;
            case R.id.tvArticleAll://推荐文章
                startActivity(ArticleActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter == hotAdapter) {
            StkResBean bean = (StkResBean) adapter.getItem(position);
            MovieDetailActivity.stkResBean = bean;
            startActivity(MovieDetailActivity.class);
        } else if (adapter == movieListAdapter) {
            ListDetailActivity.stkTagResBean = movieListAdapter.getItem(position);
            startActivity(ListDetailActivity.class);
        } else if (adapter == articleAdapter) {
            StkResBean bean = (StkResBean) adapter.getItem(position);
            BaseWebviewActivity.open(mContext, bean.getUrl(), bean.getName());
        }
    }

    @Override
    protected void initData() {
        getBannerData();
        getHotData();
        getMovieListData();
        getArticleData();
    }

    private void getBannerData() {
        StkResApi.getTagResourceList(this, AppConfig.BANNER_DATA, StkResApi.createParamMap(0, 10), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        MyBannerAdapter myMovieBannerAdapter = new MyBannerAdapter(result, mContext);
                        mDataBinding.banner.setAdapter(myMovieBannerAdapter, true);//false不无限循环
                        mDataBinding.banner.setBannerGalleryEffect(20, 20, 10, 1);
                        mDataBinding.banner.isAutoLoop(true);//false不自动轮播
                        mDataBinding.banner.setOnBannerListener(new OnBannerListener() {
                            @Override
                            public void OnBannerClick(Object data, int position) {
                                StkResBean bean = (StkResBean) mDataBinding.banner.getAdapter().getData(position);
                                MovieDetailActivity.stkResBean = bean;
                                startActivity(MovieDetailActivity.class);
                            }
                        });
                    }
                }
            }
        });
    }

    private void getHotData() {
        StkResApi.getTagResourceList(this, AppConfig.HOT_DATA, StkResApi.createParamMap(0, 3), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        hotAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void getMovieListData() {
        StkResApi.getChildTagList(this, AppConfig.MOVIE_LIST_DATA, StkResApi.createParamMap(0, 2), false, new IReqRetCallback<List<StkTagResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkTagResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        movieListAdapter.setList(result.subList(0, 2));
                    }
                }
            }
        });
    }

    private void getArticleData() {
        StkResApi.getTagResourceList(this, AppConfig.ARTICLE_DATA, StkResApi.createParamMap(0, 3), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        articleAdapter.setList(result);
                    }
                }
            }
        });
    }

}